package com.dhcc.DRGsHosp.infra.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * excel 操作工具类
 * @ClassName: ExcelUtil  
 * @Description ：    
 * @Date: 2018年3月16日 下午2:09:03  
 * @Author ChenHanlin 
 * @Version 3.1.0 DHC-DRGs
 */
public class ExcelUtil {
	
	/**
	 *  读取Excel 工作簿
	 *  readExcelSheet.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param filePath
	 *  @param index
	 *  @return 
	 *  @Date:2018年3月16日下午2:10:17
	 */
	public static Sheet readExcelSheet(String filePath,int index) {
		String extension=filePath.substring(filePath.lastIndexOf(".")).toLowerCase();//扩展名
		Workbook workbook=null;
		Sheet sheet=null;
		InputStream inputStream = null ;
		try {
			inputStream = new FileInputStream(filePath) ;
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/**
		 * 判断Excel版本，创建工作簿
		 */
		try {
			if (extension.equals(".xls")) {
				workbook=new HSSFWorkbook(inputStream);
				inputStream.close();
			} else if (extension.equals(".xlsx")){
				workbook=new XSSFWorkbook(inputStream);
			} 
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//读取工作表
		if (extension.equals(".xls")) {
			sheet=(HSSFSheet) workbook.getSheetAt(index);
		} else if(extension.equals(".xlsx")){
			sheet=(XSSFSheet)workbook.getSheetAt(index);
		} 
		return sheet;
	}
	
	/**
	 *  读取单元格数据
	 *  getCellValue.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param cell
	 *  @return 
	 *  @Date:2018年3月16日下午2:10:29
	 */
	public static String getCellValue(Cell cell) {
        String cellValue = "";
        DataFormatter formatter = new DataFormatter();
        if (cell != null) {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        cellValue = formatter.formatCellValue(cell);
                    } else {
                        double value = cell.getNumericCellValue();
                        int intValue = (int) value;
                        cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    cellValue = String.valueOf(cell.getCellFormula());
                    break;
                case Cell.CELL_TYPE_BLANK:
                    cellValue = "";
                    break;
                case Cell.CELL_TYPE_ERROR:
                    cellValue = "";
                    break;
                default:
                    cellValue = cell.toString().trim();
                    break;
            }
        }
        if(cellValue.trim().equals("")) {
        	return null ;
        } else {
        	return cellValue.trim() ;
        }
    }
	
	/**
	 *  删除指定路径的文件
	 *  deleteFile.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param filePath 
	 *  @Date:2018年3月16日下午2:10:39
	 */
	public static void deleteFile(String filePath) {
		 File file = new File(filePath);
         if(file.exists()&&file.isFile())
             file.delete();
         System.out.println(filePath + " 删除成功 ！");
	}
	
	/**
	 *  判断是否为空行
	 *  isRowEmpty.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param row
	 *  @return 
	 *  @Date:2018年3月15日下午10:22:29
	 */
	public static boolean isRowEmpty(Row row) {
	   for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
	       Cell cell = row.getCell(c);
	       if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
	           return false;
	   }
	   return true;
	}
}
